package com.tianqingwl.gsystem.template.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tianqingwl.gsystem.common.vo.CommonResultVo;
import com.tianqingwl.gsystem.common.vo.PageResultVo;
import com.tianqingwl.gsystem.template.pojo.SysTemplate;
import com.tianqingwl.gsystem.template.service.SysTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/manage/template/")
@PreAuthorize("@gs.check('sys_template_manage')")
public class SysTemplateController {
    @Autowired
    private SysTemplateService sysTemplateService;

    @RequestMapping("/upload")
    @ResponseBody
    public Object upload(MultipartFile[] file){
        if (file==null||file.length<1) {
            return CommonResultVo.fail("file empty");
        }
        CommonResultVo saveResult = sysTemplateService.saveFile(file);

        return saveResult;
    }
    @GetMapping("template-manage.html")
    public String templateManagePage(){
        return "manage/template/template-manage";
    }

    @GetMapping("/list")
    @ResponseBody
    public Object list(Long page, Long limit){
        page = page == null ? 1 : page;
        limit = limit == null ? 10 : limit;

        Page<SysTemplate> sysTemplatePage = new Page<>(page, limit);
        sysTemplateService.listSysTemplate(sysTemplatePage);
        return PageResultVo.success(sysTemplatePage.getTotal(),sysTemplatePage.getPages(), sysTemplatePage.getRecords());
    }

    @GetMapping("/delete")
    @ResponseBody
    public Object delete(Long templateId){
        if (templateId==null){
            return CommonResultVo.fail("no id");
        }
        try {
            sysTemplateService.delete(templateId);
        }catch (Exception e){
            return CommonResultVo.fail(e.getMessage());
        }
        return CommonResultVo.success();
    }

    @GetMapping("/enable")
    @ResponseBody
    public Object enable(Long templateId){
        sysTemplateService.updateStatus(templateId);
        return CommonResultVo.success();
    }


}
